package com.pyb.module.live;

import android.app.Activity;
import android.util.Log;

import com.easefun.polyv.livecommon.module.config.PLVLiveChannelConfigFiller;
import com.easefun.polyv.livecommon.module.utils.result.PLVLaunchResult;
import com.easefun.polyv.livescenes.config.PolyvLiveChannelType;
import com.easefun.polyv.livescenes.feature.login.IPLVSceneLoginManager;
import com.easefun.polyv.livescenes.feature.login.PLVSceneLoginManager;
import com.easefun.polyv.livescenes.feature.login.model.PLVSLoginVO;
import com.easefun.polyv.livestreamer.scenes.PLVLSLiveStreamerActivity;
import com.easefun.polyv.streameralone.scenes.PLVSAStreamerAloneActivity;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.commonutil.activity.ActivityManager;

public class OpenLiveInstance {

    private static OpenLiveInstance instance;
    private IPLVSceneLoginManager loginManager;
    public static String appId = "eijrru3it3";
    public static String appSecret = "9893f17fc53e4b3b9beaf72bfcb3f9c6";
    public static String userId = "e64btvy51d";
    public static String channelId = "108758";

    public static OpenLiveInstance getInstance(){
        if (instance == null){
            synchronized (OpenLiveInstance.class){
                if (instance == null){
                    instance = new OpenLiveInstance();
                }
            }
        }
        return instance;
    }

    /**
     * 开直播
     * @param channelId 频道Id
     * @param pwd 密码
     */
    private void openLive(String channelId,String pwd){
        if (loginManager == null)loginManager = new PLVSceneLoginManager();
        loginManager.loginStreamer("108758", "387637",
                new IPLVSceneLoginManager.OnStringCodeLoginListener<PLVSLoginVO>() {
                    @Override
                    public void onLoginSuccess(PLVSLoginVO loginVO) {
//                updateLoginViewStatus(false);
//                if (plvlsLoginRememberPasswordCb.isChecked()) {
//                    localInfoManager.saveLoginInfo(channelId, password, nick, true);
//                } else {
//                    localInfoManager.saveLoginInfo("", "", "", false);
//                }

                        //更新开播状态
                        PLVLiveChannelConfigFiller.setLiveStreamingWhenLogin(loginVO.isLiveStatus());

                        //不填写登录昵称时，使用登录接口返回的后台设置的昵称
                        String loginNick = loginVO.getTeacherNickname();

                        PolyvLiveChannelType liveChannelType = loginVO.getLiveChannelType();
                        Log.i("inffs", "onLoginSuccess: " + liveChannelType.name());
                        if (PolyvLiveChannelType.PPT.equals(liveChannelType)) {
                            //进入手机开播三分屏场景
                            boolean isOpenCamera = "N".equals(loginVO.getIsOnlyAudio());
                            PLVLaunchResult launchResult = PLVLSLiveStreamerActivity.launchStreamer(
                                    ActivityManager.last(),
                                    loginVO.getChannelId(),
                                    loginVO.getInteractUid(),
                                    loginNick,
                                    loginVO.getTeacherAvatar(),
                                    loginVO.getTeacherActor(),
                                    loginVO.getRole(),
                                    loginVO.getColinMicType(),
                                    true,
                                    isOpenCamera,
                                    true
                            );
                            if (!launchResult.isSuccess()) {
                                onLoginFailed(launchResult.getErrorMessage(), launchResult.getError());
                            }
                        } else if (PolyvLiveChannelType.ALONE.equals(liveChannelType)) {
                            //进入手机开播纯视频场景
                            PLVLaunchResult launchResult = PLVSAStreamerAloneActivity.launchStreamer(
                                    (Activity) ActivityManager.last(),
                                    loginVO.getChannelId(),
                                    loginVO.getAccountId(),
                                    loginNick,
                                    loginVO.getTeacherAvatar(),
                                    loginVO.getTeacherActor(),
                                    loginVO.getChannelName()
                            );
                            if (!launchResult.isSuccess()) {
                                onLoginFailed(launchResult.getErrorMessage(), launchResult.getError());
                            }
                        } else {
//                    String errorMsg = getResources().getString(R.string.plv_scene_login_toast_streamer_no_support_type);
//                    onLoginFailed(errorMsg, new Throwable(errorMsg));
                        }
                    }

                    @Override
                    public void onLoginFailed(String msg, Throwable throwable) {
                        onLoginFailed("无法开播：" + msg, "", throwable);
//                        ToastHelper.show(msg);
                    }

                    @Override
                    public void onLoginFailed(String msg, String code, Throwable throwable) {
                        throwable.printStackTrace();
                        Log.i("inffs", "onLoginFailed: " + msg);
                    }
                });
    }

    public void destroy() {
        if (loginManager != null) {
            loginManager.destroy();
        }
    }

    public String getViewerId() {
        /**
         *  todo 请务必在这里替换为你的学员(用户)ID，设置学员(用户)ID的意义详细可以查看：https://github.com/polyv/polyv-android-cloudClass-sdk-demo/wiki/6-%E8%AE%BE%E7%BD%AE%E5%AD%A6%E5%91%98%E5%94%AF%E4%B8%80%E6%A0%87%E8%AF%86%E7%9A%84%E6%84%8F%E4%B9%89
         */
//        return PLVUtils.getAndroidId(ActivityManager.last()) + "";
        return CacheUserInfo.getInstance().getId() + "";
    }

    public String getViewerName() {
        /**
         * todo 请务必在这里替换为你的学员(用户)昵称
         */
//        return "观众" + getViewerId();
        return CacheUserInfo.getInstance().getsUserInfo().getUser().getRname();
    }

    public String getViewerAvatar() {
        //todo 在这里可替换为你的学员(用户)头像地址
//        return "https://img2.baidu.com/it/u=1070003001,653753576&fm=26&fmt=auto&gp=0.jpg";
        return CacheUserInfo.getInstance().getsUserInfo().getUser().getAvatar();
    }
}
